package cn.com.dycc.meeting_assist.meeting_room.service;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import cn.com.dycc.meeting_assist.meeting_room.dao.RoomDao;
import cn.com.dycc.meeting_assist.meeting_room.entity.Room;
import cn.com.dycc.meeting_assist.meeting_room.param.FindRoomParam;
import cn.com.dycc.util.DateUtil;
import cn.com.dycc.util.Paging;

/**
 * 
 * Description: 会议室管理业务层实现
 * Author: Liket
 * Version: 1.0
 * Create Date Time: 2018年10月25日 上午10:16:03.
 * Update Date Time: 
 * @see
 */
@Service("roomService")
public class RoomServiceImpl implements RoomService{
	
	@Resource
	private RoomDao roomDao;
	
	@Resource(name="paging")
	private Paging paging;
	
		
	public int addRoom(HttpServletRequest req, Room room)
			throws RoomNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		String user_id = (String)session.getAttribute("id");
		if(org_id==null){
			throw new RoomNotFoundException("请重新登录");
		}
		if(room==null){
			throw new RoomNotFoundException("请填写会议室信息");
		}
				
		if(room.getRoom_name()==null||room.getRoom_name().trim().isEmpty()){
			throw new RoomNotFoundException("请填写名称");
		}
		if(room.getUser_flag()==null||room.getUser_flag().trim().isEmpty()){
			throw new RoomNotFoundException("请填写使用标识");
		}
		
		room.setOrg_id(org_id);
		room.setCreate_date(DateUtil.getNowDate(DateUtil.TIME_FMT));
		room.setOpt_user_id(user_id);
		
		int n = roomDao.addRoom(room);
		if(n!=1){
			throw new RoomNotFoundException("系统繁忙，添加失败");
		}
		return n;
	}

	public int deleteRoom(HttpServletRequest req, String room_id) throws RoomNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new RoomNotFoundException("请重新登录");
		}
		if(room_id==null||room_id.trim().isEmpty()){
			throw new RoomNotFoundException("请选择删除的会议室");
		}
		int n = roomDao.deleteRoom(room_id,org_id);
		if(n!=1){
			throw new RoomNotFoundException("系统繁忙，删除失败");
		}
		return n;
	}

	public int updateRoom(HttpServletRequest req, Room room)
			throws RoomNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		String user_id = (String)session.getAttribute("id");
		if(org_id==null){
			throw new RoomNotFoundException("请重新登录");
		}
		if(room==null){
			throw new RoomNotFoundException("请填写会议室信息");
		}
				
		if(room.getRoom_name()==null||room.getRoom_name().trim().isEmpty()){
			throw new RoomNotFoundException("请填写名称");
		}
		if(room.getUser_flag()==null||room.getUser_flag().trim().isEmpty()){
			throw new RoomNotFoundException("请填写使用标识");
		}		
		Room oldRoom = roomDao.findRoomById(room.getRoom_id(),org_id);
		if(oldRoom==null){
			throw new RoomNotFoundException("系统繁忙，修改失败");
		}
		
		Room newRoom = new Room();
		if(!room.getRoom_name().equals(oldRoom.getRoom_name())){
			newRoom.setRoom_name(room.getRoom_name());
		}
		if(room.getRoom_address()!=null){
			if(!room.getRoom_address().equals(oldRoom.getRoom_address())){
				newRoom.setRoom_address(room.getRoom_address());
			}
		}			
		if(!room.getRoom_des().equals(oldRoom.getRoom_des())){
			newRoom.setRoom_des(room.getRoom_des());
		}
				
		if(!room.getUser_flag().equals(oldRoom.getUser_flag())){
			newRoom.setUser_flag(room.getUser_flag());
			
		}
		if(room.getState()!=null){
			if(!room.getState().equals(oldRoom.getState())){
				newRoom.setState(room.getState());
			}
		}	
		
		newRoom.setOpt_user_id(user_id);		
		newRoom.setRoom_id(room.getRoom_id());
		newRoom.setOrg_id(org_id);
		int n = roomDao.updateRoom(newRoom);
		if(n!=1){
			throw new RoomNotFoundException("系统繁忙，稍后再试");
		}
		return n;
	}

	public Room findRoomById(HttpServletRequest req, String room_id)
			throws RoomNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new RoomNotFoundException("请重新登录");
		}
		if(room_id==null||room_id.trim().isEmpty()){
			throw new RoomNotFoundException("会议室不存在");
		}
		Room room = roomDao.findRoomById(room_id,org_id);
		if(room==null){
			throw new RoomNotFoundException("系统繁忙，查询失败");
		}
		return room;
	}

	public List<Room> findRoomByParam(HttpServletRequest req,FindRoomParam param)
			throws RoomNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new RoomNotFoundException("请重新登录");
		}
		Integer page = param.getPage();
		Integer limit = param.getLimit();
		Integer start = null;
		if(limit==null&&page!=null){
			limit = paging.getPageSize();
			start=(page-1)*limit;
		}
		if(page==null){
			start = null;
			limit = null;
		}
		if(limit!=null&&page!=null){
			start = limit*(page-1);
		}		
		param.setStart(start);	
		param.setOrg_id(org_id);
		List<Room> list = roomDao.findRoomByParam(param);
		return list;
	}

	public String countRoomByParam(HttpServletRequest req, FindRoomParam param)
			throws RoomNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new RoomNotFoundException("请重新登录");
		}
		param.setOrg_id(org_id);
		String count = roomDao.countRoomByParam(param);
		return count;
	}

	
}
